<template>
  <el-dialog
    customClass="dialogDSClass"
    title="数据源权限"
    :visible.sync="showDSViewDialog"
    @close="close"
    ref="tree"
    append-to-body="true">
    <el-container>
      <el-main>
        <el-tree disabled
          :data="dsTree"
          show-checkbox
          default-expand-all
          :default-checked-keys="checkTree"
          node-key="id"
          ref="tree"
          highlight-current
          :props="dsProps">
        </el-tree>
      </el-main>
    </el-container>
  </el-dialog>
</template>
<script>
import sysAccreditApi from "./api/sysAccredit.service";

export default {
  data() {
    return {
      showDSViewDialog: false,
      dsProps: {
        children: "children",
        label: "label"
      },
      roleId: "",
      checkTree: [],
      dsTree:[]
    };
  },
  methods: {

    /**
     * 数据权限
     * @param id
     * @returns {Promise<void>}
     */
    async showDSView(id) {
      this.roleId = id;
      this.checkTree = [];
      //tree
      let res = await sysAccreditApi.getDSTree();
      this.dsTree = res.data;
      let res2 = await sysAccreditApi.getRoleDSTree(id);
      this.$nextTick(() => {
        this.checkTree = this.resolveAllChildrenNodeId(this.dsTree,res2,[]);
      })
      this.showDSViewDialog = true;
    },
    close() {
      this.checkTree = [];
      this.showDSViewDialog = false;
    },

    /**
     * 递归选中
     * @param json
     * @param idArr
     * @param temp
     * @returns {*}
     */
    resolveAllChildrenNodeId(json, idArr, temp) {
      for (let i = 0; i < json.length; i++) {
        const item = json[i];
        if (item.children && item.children.length !== 0) {
          this.resolveAllChildrenNodeId(item.children, idArr, temp)
        } else {
          let id = idArr.filter(id => id === item.id);
          temp.push(id)
        }
      }
      return temp;
    },
  }
};
</script>
